package com.jeff.android.lab.architecture.networklib;

import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.jack.network.NetChangeObserver;
import com.jack.network.NetWorkType;
import com.jack.network.NetworkManager;
import com.jack.network.annotation.Network;
import com.jack.network.utils.Constants;
import com.jeff.android.lab.BaseFragment;
import com.jeff.android.lab.R;

import butterknife.BindView;

/**
 * @author: dingfeng
 * @date: 2020-04-12
 * @since: 1.0.0
 **/
public class NetworklibFragment extends BaseFragment implements NetChangeObserver {

    @BindView(R.id.tv_log)
    TextView mTvLog;

    private StringBuilder sb = new StringBuilder("");

    @Override
    protected int getRootLayout() {
        return R.layout.architecture_networklib_fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//        NetworkManager.getDefault().init(getActivity().getApplication());
//        NetworkManager.getDefault().setListener(this);

        NetworkManager.getDefault().registerObserver(this);
    }

    @Override
    protected void initView() {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        NetworkManager.getDefault().unRegisterObserver(this);
    }

    @Override
    public void onConnect(NetWorkType type) {
        Log.d(Constants.TAG, "连接了  " + type.name());
    }

    @Override
    public void onDisconnect() {
        Log.d(Constants.TAG, "没网络");
    }

    @Network(netType = NetWorkType.AUTO)
    public void network(NetWorkType type) {
        switch (type) {
            case MOBILE:
                Log.d(Constants.TAG, "手机网络");
                sb.append("手机网络");
                break;

            case WIFI:
                Log.d(Constants.TAG, "WI-FI");
                sb.append("WI-FI");
                break;

            case NONE:
                Log.d(Constants.TAG, "没网络");
                sb.append("没网络");
                break;
        }
        printLog();
    }

    private void printLog() {
        mTvLog.append(sb.append("\n").toString());
    }

}
